//
//  AMButton.m
//  sociallanguage
//
//  Created by Fox Guo on 14-10-24.
//  Copyright (c) 2014年 Contra. All rights reserved.
//

#import "HOButton.h"
#import "HOUtils.h"
#import "Logger.h"
@implementation HOButton
@synthesize data;
@synthesize isSelected;


- (void)setLeftImage:(UIImage *)img
{
//	if(leftImgView == nil)
//	{
//		leftImgView = [[UIImageView alloc] initWithFrame:CGRectMake(self.frame.origin.x + 30,
//																	self.frame.origin.y + (self.frame.size.width - img.size.width) / 2,
//																	img.size.width,
//																	img.size.height)];
//		[[self superview] addSubview:leftImgView];
//		[[self superview] bringSubviewToFront:leftImgView];
//		leftImgView.layer.borderWidth = 1;
//	}
//    self.leftImg = img;
//	leftImgView.image = img;

}
- (void)setRightImage:(UIImage *)img withTitle:(NSString *)title
{

    [super setTitle:title forState:UIControlStateNormal];
    [super setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [super setTitleColor:[UIColor grayColor] forState:UIControlStateHighlighted];
    [super setTitleColor:[UIColor grayColor] forState:UIControlStateSelected];
    [super setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
    if(rightImgView == nil)
    {
        rightImgView = [[UIImageView alloc]
                            initWithFrame:CGRectMake([self getSystemTitleWidth],
                                                     (self.frame.size.height - img.size.height) / 2,
                                                     img.size.width,
                                                     img.size.height)];
        [self addSubview:rightImgView];
    }
        rightImgView.image = img;
    
    
    self.frame = CGRectMake(self.frame.origin.x,
                            self.frame.origin.y,
                            rightImgView.frame.size.width + [self getSystemTitleWidth],
                            self.frame.size.height);
    [self setNeedsDisplay];
}


- (void)setRightImage:(UIImage *)img
                    withTitle:(NSString *)title
                withTextColor:(UIColor *)textColor
withTextHighlightColor:(UIColor *)highlightColor
             selector:(SEL)selector
               target:(id)target
{
    
    [self setTitle:title forState:UIControlStateNormal];
    [self setTitleColor:textColor forState:UIControlStateNormal];
    [self setTitleColor:highlightColor forState:UIControlStateHighlighted];
    [self setTitleColor:highlightColor forState:UIControlStateSelected];
    [self setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
    [self addTarget:target action:selector forControlEvents:UIControlEventTouchUpInside];
    if(rightImgView == nil)
    {
        rightImgView = [[UIImageView alloc]
                        initWithFrame:CGRectMake([self getTitleWidth] + 18,
                                                 (self.frame.size.height - img.size.height) / 2,
                                                 img.size.width,
                                                 img.size.height)];
        [self addSubview:rightImgView];
    }
    rightImgView.image = img;
    
    
    self.frame = CGRectMake(self.frame.origin.x,
                            self.frame.origin.y,
                            rightImgView.frame.size.width + 18 + [self getTitleWidth],
                            self.frame.size.height);
    [self setNeedsDisplay];
}

//- (void)setTitle:(NSString *)title forState:(UIControlState)state
//{
//    
//    [self setButtonTitle:title];
//    if(rightImgView)
//    {
//        
//        rightImgView.frame = CGRectMake([self getTitleWidth] + 18,
//                                        (self.frame.size.height - rightImgView.frame.size.height) / 2,
//                                        rightImgView.frame.size.width,
//                                        rightImgView.frame.size.height);
//        
//        
//        self.frame = CGRectMake(self.frame.origin.x,
//                                self.frame.origin.y,
//                                rightImgView.frame.size.width + 18 + [self getTitleWidth],
//                                self.frame.size.height);
//        [self setNeedsDisplay];
//    }
//    else
//    {
//        
//        self.frame = CGRectMake(self.frame.origin.x,
//                                self.frame.origin.y,
//                                self.frame.size.width,
//                                self.frame.size.height);
//        [self setNeedsDisplay];
//    }
//}


- (void)setTopImage:(UIImage *)img
{
    if(topImgView == nil)
    {
        CGFloat offset = (self.frame.size.width - img.size.width) / 2;
        topImgView = [[UIImageView alloc]
                      initWithFrame:CGRectMake( offset < 0 ? 0 : offset,
                                                 5,
                                                 img.size.width,
                                                 img.size.height)];
        [self addSubview:topImgView];
    }
    topImgView.image = img;
    
    
    customTitleLbl.frame = CGRectMake(customTitleLbl.frame.origin.x,
                                      topImgView.frame.origin.y + topImgView.frame.size.height + 5,
                                      customTitleLbl.frame.size.width,
                                      customTitleLbl.frame.size.height);
    
    [self setNeedsDisplay];
}

- (void)setButtonTitleFont:(UIFont *)font
{
    if(customTitleLbl == nil)
    {
        return;
    }
    else
    {
        [customTitleLbl setFont:font];
        [self updateText];
    }
}
- (CGFloat)getSystemTitleWidth
{
    CGSize size = CGSizeZero;
    NSString *title = self.titleLabel.text;
    if(IS_LESSTHAN_IOS7)
    {
        size = [title sizeWithFont:self.titleLabel.font];
    }
    else
    {
        
        NSDictionary* dic = [NSDictionary
                             dictionaryWithObjectsAndKeys:
                             NSForegroundColorAttributeName,UIColorFromRGB(0xff0000),
                             self.titleLabel.font, NSFontAttributeName, nil];
        size = [title sizeWithAttributes:dic];
    }
    return size.width;
}

- (CGFloat)getTitleWidth
{
    CGSize size = CGSizeZero;
    NSString *title = self.titleLabel.text;
    if(IS_LESSTHAN_IOS7)
    {
        size = [title sizeWithFont:customTitleLbl.font];
    }
    else
    {
        
        NSDictionary* dic = [NSDictionary
                             dictionaryWithObjectsAndKeys:
                             NSForegroundColorAttributeName,UIColorFromRGB(0xff0000),
                             self.titleLabel.font, NSFontAttributeName, nil];
        size = [title sizeWithAttributes:dic];
    }
    return size.width;
}

- (void)updateText
{
    if(customTitleLbl.text != nil)
    {
        CGSize size = CGSizeZero;
        if(IS_LESSTHAN_IOS7)
        {
            size = [customTitleLbl.text sizeWithFont:customTitleLbl.font];
        }
        else
        {
            size = [customTitleLbl.text sizeWithAttributes:@{ NSFontAttributeName:customTitleLbl.font}];
        }
//        LogInf(@"size: w:%f, height:%f", size.width, size.height);
        customTitleLbl.frame = CGRectMake(0, (self.frame.size.height - size.height) / 2, self.frame.size.width, size.height);
    }
}
- (void)setButtonTitleColor:(UIColor *)color
{
    if(customTitleLbl == nil)
    {
        return;
    }
    else
    {
        [customTitleLbl setTextColor:color];
    }
}

- (void)setStyleWithTitle:(NSString *)title
               titleColor:(UIColor *)titleColor
                titleFont:(UIFont *)font
               norBgColor:(UIColor *)norBgColor
               selBgColor:(UIColor *)selBgColor
          highlighBgColor:(UIColor *)highlightBgColor
                 selector:(SEL)selector
                   target:(id)target
{
    [self setButtonTitle:title];
    [self setButtonTitleColor:titleColor];
    [self setButtonTitleFont:font];
    [self setImage:[HOUtils createImageWithColor:norBgColor forView:self] forState:UIControlStateNormal];
    if(selBgColor)
        [self setImage:[HOUtils createImageWithColor:selBgColor forView:self] forState:UIControlStateSelected];
    if(highlightBgColor)
        [self setImage:[HOUtils createImageWithColor:highlightBgColor forView:self] forState:UIControlStateHighlighted];
    [self addTarget:target action:selector forControlEvents:UIControlEventTouchUpInside];
}

- (void)setButtonTitle:(NSString *)title
{
    if(customTitleLbl == nil)
    {
//        [super setTitle:title forState:UIControlStateNormal];
        [self updateText];
        customTitleLbl=  [[UILabel alloc] initWithFrame:CGRectMake(0, (self.frame.size.height - 20) / 2, self.frame.size.width, 20)];
        customTitleLbl.textAlignment = NSTextAlignmentCenter;
//        customTitleLbl.layer.borderWidth = 1;
        [self addSubview:customTitleLbl];
    }
    customTitleLbl.text = title;
    [self setNeedsDisplay];
}

- (void)dealloc
{
    [topImgView release];
    self.leftImg = nil;
    [customTitleLbl release];
	if(leftImgView)
	{
		[leftImgView removeFromSuperview];
		[leftImgView release];
	}
	if(rightImgView)
		[rightImgView release];
    
    [super dealloc];
}

@end
